package com.example.module_device.ui

import android.os.Bundle
import android.view.MenuItem
import android.widget.PopupMenu
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import com.example.lib_common.base.BaseActivity
import com.example.lib_common.base.state.Failed
import com.example.lib_common.base.state.Success
import com.example.lib_common.observeWithLifecycle
import com.example.module_device.DeviceViewModel
import com.example.module_device.R
import com.example.module_device.databinding.ActivityAddDeviceBinding
import com.example.module_device.model.DeviceEntityItem
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class AddDeviceActivity : BaseActivity<DeviceViewModel,ActivityAddDeviceBinding>() {

    private val mViewModel : DeviceViewModel by viewModels()
    override val mLayoutResId: Int = R.layout.activity_add_device

    override fun initView() {
        super.initView()
    }

    override fun initData() {
        super.initData()
        mBinding.typeText.setOnClickListener {
            setTypeText()
        }
        mBinding.addText.setOnClickListener {
            setAddText()
        }
        mBinding.timeText.setOnClickListener {
            setTimeText()
        }
    }


    override fun initListener() {
        super.initListener()
        mBinding.submitButton.setOnClickListener {
            val type = mBinding.typeText.text.toString()
            val add = mBinding.addText.text.toString()
            val time = mBinding.timeText.text.toString()
            val param = DeviceEntityItem(type,type,0.16,0,add,"正常")
            mViewModel.getAddDevice(param)
            mViewModel.mAddDeviceState.observeWithLifecycle(this){
                when(it){
                    is Success -> {
                        showToast("添加成功")
                        onBackPressed()
                    }
                    is Failed -> {
                        showToast("添加成功")
                        onBackPressed()
                    }
                    else -> {}
                }
            }

        }

    }

    private fun setTypeText(){
        val items = arrayOf("门禁设备", "监控设备", "电梯设备")
        MaterialAlertDialogBuilder(this)
            .setTitle("选择工单类型")
            .setItems(items) { _, which ->
                mBinding.typeText.setText(items[which])
            }
            .show()
    }
    private fun setAddText(){
        val items = arrayOf("东门", "南门", "西门","北门")
        MaterialAlertDialogBuilder(this)
            .setTitle("选择地址")
            .setItems(items) { _, which ->
                mBinding.addText.setText(items[which])
            }
            .show()
    }
    private fun setTimeText(){
        val items = arrayOf("7:00 - 8:00", "11:00 - 12:00", "14:00 - 15:00","17:00 - 18:00")
        MaterialAlertDialogBuilder(this)
            .setTitle("选择时间段")
            .setItems(items) { _, which ->
                mBinding.timeText.setText(items[which])
            }
            .show()
    }
}